package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.EmergencyDao;
import cn.hls.entity.Emergency;
import cn.hls.entity.vo.PageInfo;

@Component("emergencyDao")
public class EmergencyDaoImpl extends BaseDaoImpl<Emergency> implements
		EmergencyDao {

	@Override
	public boolean exists(Emergency entity) throws Exception {
		StringBuilder hql=new StringBuilder("from Emergency p where p.deleted=? ");
		List<Object>list=new ArrayList<Object>();
		list.add(false);
		if(entity!=null){
			if(StringUtils.hasText(entity.getEmergencyName())){//应急名称条件查询
				hql.append("and p.emergencyName=? "	);
				list.add(entity.getEmergencyName());
			}
			if(StringUtils.hasText(entity.getEmergencyId())){//应急编号条件查询
				hql.append("and p.emergencyId=? ");
				list.add(entity.getEmergencyId());
			}
		}
		Long c= countByHql(hql.toString(), list.toArray());
		return (c!=null && c.longValue()>0)?true:false;
 
	}

	@Override
	public PageInfo<Emergency> loadEmergencyInfo(PageInfo<Emergency> page,
			Emergency entity) throws Exception {
		//定义查询sql语句
		StringBuilder selectHql=new StringBuilder("from Emergency p where p.deleted=? ");
		//定义统计sql语句
		StringBuilder countHql=new StringBuilder("select count(p.id) ");
		//定义查询条件
		List<Object>params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按应急编号查询
			if(StringUtils.hasText(entity.getEmergencyId())){
				selectHql.append("and p.emergencyId like ? ");
				params.add("%"+entity.getEmergencyId().trim()+"%");
			}
			//按应急名称查询
			if(StringUtils.hasText(entity.getEmergencyName())){
				selectHql.append("and p.emergencyName like ? ");
				params.add("%"+entity.getEmergencyName().trim()+"%");
			}
			//危险品名称查询
			if(StringUtils.hasText(entity.getDangerGoodsName())){
				selectHql.append("and p.dangerGoodsName like ? ");
				params.add("%"+entity.getDangerGoodsName().trim()+"%");
			}
		}
		countHql.append(selectHql);
		selectHql.append("order by id desc ");
		return find(selectHql.toString(), countHql.toString(), params.toArray(), page);
	}

}
